
import VueRouter from 'vue-router'
import About from '../pages/About'
import Home from '../pages/Home'
import Message from '../pages/Message'
import News from '../pages/News'
import Detail from '../pages/Detail'

const router = new VueRouter({
    routers:[
        {
            path:"/about",
            component:About,
            meta:{
                isAuth:false,
                title:"关于",
            },
        },
        {
            path:"/home",
            component:Home,
            meta:{
                isAuth:false,
                title:"主页",
            },
            children:[
                {
                    path:"message",
                    name:"xiaoxi",
                    component:Message,
                    // route里面只能往meta里面放自定义信息
                    meta:{
                        isAuth:true,
                        title:"消息",
                    },
                    children:[
                        {
                            name:"xiangqing",
                            path:"detail/:id/:title",
                            component:Detail,
                            meta:{
                                isAuth:true,
                                title:"详情",
                            },
                            props(route){
                                return {
                                    id:route.params.id,
                                    title:route.params.title,
                                }
                            },
                        }
                    ]
                },
                {
                    path:"news",
                    name:"xinwen",
                    component:News,
                    // route里面只能往meta里面放自定义信息
                    // meta:{isAuth:true},
                    meta:{
                        isAuth:true,
                        title:"新闻",
                    },
                },
            ]
        },
    ]
})

// 全局前置路由守卫
// 初始化的时候被调用
// 每次切换之前被调用
router.beforeEach((to,from,next)=>{
    console.log(to,from,next)
    // 如果meta里面有配置这个的话
    if(to.meta.isAuth){
        if(localStorage.getItem("token")==="zzzzzzzzzzzzzzz"){
            next()
        }else{
            alert("无权查看")
        }
    }else{
        next()
    }
})

// 全局后置路由守卫
// 初始化的时候被调用
// 每次切换之后被调用
router.afterEach((to,from)=>{
    console.log(to,from,next)
    document.title = to.meta.title
})

export default router